package cn.edu.nhic.controller;

import cn.edu.nhic.constant.Constants;
import cn.edu.nhic.entity.User;
import cn.edu.nhic.service.CheckService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description
 * @Date 2024/9/14 15:03
 * @Created by dwp
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter(Constants.ParamsKeyEnum.USER_NAME_KEY.getParamKey());
        String password = req.getParameter(Constants.ParamsKeyEnum.PASSWORD_KEY.getParamKey());

        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);

        CheckService checkService = CheckService.getCheckService();
        boolean existUser = checkService.checkLogin(user);
        if (existUser) {
            // fixme: 使用的forward跳转,后续修改成include, sendRedirect()看看三者的区别
            req.getRequestDispatcher("success.jsp").forward(req, resp);
        } else {
            // fixme: 使用的forward跳转,后续修改成include, sendRedirect()看看三者的区别
            req.getRequestDispatcher("failure.jsp").forward(req, resp);
        }

    }
}
